package de.karlNet.sshTray.gui.tableModels;

import java.util.List;

import javax.naming.directory.InvalidAttributeIdentifierException;
import javax.swing.table.AbstractTableModel;

import org.springframework.stereotype.Component;

import de.karlNet.sshTray.datasources.IDataSource;
import de.karlNet.sshTray.model.Connection;

@Component
public class ConnectionsTableModel extends AbstractTableModel {
	private List<Connection> connections;

	public int getColumnCount() {
		return 3;
	}

	public int getRowCount() {
		if(this.connections.size()<=0) {
			return 1;
		}
		return this.connections.size();
	}

	@Override
	public String getColumnName(int col) {
		switch(col) {
		case 0:
			return "Title";
		case 1:
			return "Hostname";
		case 2:
			return "Username";
		default:
			throw new IllegalStateException("Undefined Column");
		}
	}
	
	public Connection getConnectionAt(int row) {
		return this.connections.get(row);
	}

	public Object getValueAt(int arg0, int arg1) {
		if(this.connections.size()<=0) {
			switch (arg1) {
			case 0:
				return "-No hosts defined-";
			case 2:
			case 1:
				return "";
			default:
				throw new IllegalStateException("Undefined Column");
			}
		}
		Connection con = this.connections.get(arg0);
		switch (arg1) {
		case 0:
			return con.getTitle();
		case 1:
			return con.getHost();
		case 2:
			return con.getUsername();
		default:
			throw new IllegalStateException("Undefined Column");
		}
	}

	public void refresh(List<Connection> connections) {
		this.connections = connections;
		this.fireTableDataChanged();
	}
	
	public ConnectionsTableModel() {
	}
}
